package cnpm02.j2ee.group3.logic.security;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Identity;

import cnpm02.j2ee.group3.logic.dao.impl.PersonDao;
import cnpm02.j2ee.group3.logic.utils.DESUtil;
import cnpm02.j2ee.group3.model.Person;

@Name("authenticator")
public class AuthenticatorService {

	@Logger
	protected Log log;
	@In
	private Identity identity;
	@In
	private PersonDao personDao;

	public boolean authenticate() {
		log.info("begin authenticating");
		boolean succeeded = false;
		String username = identity.getCredentials().getUsername();
		String password = identity.getCredentials().getPassword();
		Person found = personDao.find(username);
		DESUtil des = DESUtil.getInstance(found.getKey());
		String pPassword = des.decrypt(found.getPassword());
		if (password.equals(pPassword)) {
			succeeded = true;
		}
		log.debug("end authengticating");
		return succeeded;
	}

	public void setIdentity(Identity identity) {
		this.identity = identity;
	}

	public Identity getIdentity() {
		return identity;
	}
}
